package flex.cc.uac.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import flex.cc.PublicUtil;
import flex.cc.base.constant.GlobalConstant;
import flex.cc.base.enums.ErrorCodeEnum;
import flex.cc.uac.dao.UacRoleUserDao;
import flex.cc.uac.entity.UacRoleUserEntity;
import flex.cc.uac.exceptions.UacBizException;
import flex.cc.uac.service.UacRoleUserService;
import flex.cc.uac.vo.RoleUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("uacRoleUserService")
@Transactional(rollbackFor = Exception.class)
public class UacRoleUserServiceImpl extends ServiceImpl<UacRoleUserDao, UacRoleUserEntity> implements UacRoleUserService {

    @Autowired
    private UacRoleUserDao uacRoleUserDao;

    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public List<UacRoleUserEntity> listByRoleId(Long roleId) {
        if (roleId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("role_id",roleId);
        return this.listByMap(map);
    }

    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public List<UacRoleUserEntity> listByUserId(Long userId) {
        if (userId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10011001);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("user_id",userId);
        return this.listByMap(map);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        Preconditions.checkArgument(roleId != null, ErrorCodeEnum.UAC10012001.msg());
        Preconditions.checkArgument(!Objects.equals(roleId, GlobalConstant.Sys.SUPER_MANAGER_ROLE_ID), "超级管理员角色不能删除");

        Map<String,Object> map = new HashMap<>();
        map.put("role_id",roleId);

        boolean result = this.removeByMap(map);
        if(!result){
            throw new UacBizException(ErrorCodeEnum.UAC10012006, roleId);
        }
    }

    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public List<UacRoleUserEntity> listByRoleIdList(List<Long> idList) {
        if (PublicUtil.isEmpty(idList)) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }
        Wrapper queryWrapper = new QueryWrapper<UacRoleUserEntity>()
                .in("role_id",idList);
        return this.list(queryWrapper);
    }

    @Override
    public void deleteByRoleIdList(List<Long> roleIdList) {
        Preconditions.checkArgument(PublicUtil.isNotEmpty(roleIdList), ErrorCodeEnum.UAC10012001.msg());
        Preconditions.checkArgument(!roleIdList.contains(GlobalConstant.Sys.SUPER_MANAGER_ROLE_ID), "超级管理员角色不能删除");
        Wrapper queryWrapper = new QueryWrapper<UacRoleUserEntity>()
                .in("role_id",roleIdList);
        boolean result = this.remove(queryWrapper);
        if (!result) {
            throw new UacBizException(ErrorCodeEnum.UAC10012007, Joiner.on(GlobalConstant.Symbol.COMMA).join(roleIdList));
        }
    }

    @Override
    public UacRoleUserEntity getByUserIdAndRoleId(Long userId, Long roleId) {
        if (null == userId) {
            throw new UacBizException(ErrorCodeEnum.UAC10011001);
        }
        if (null == roleId) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }
        Wrapper queryWrapper = new QueryWrapper<UacRoleUserEntity>()
                .eq("user_id",userId)
                .eq("role_id",roleId);
        return this.getOne(queryWrapper);
    }

    @Override
    public boolean saveRoleUser(Long userId, Long roleId) {
        if (userId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10011001);
        }
        if (roleId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }

        UacRoleUserEntity roleUser = new UacRoleUserEntity();
        roleUser.setUserId(userId);
        roleUser.setRoleId(roleId);
        return this.save(roleUser);
    }

    public IPage<RoleUserVo> getRoleUserWithPage(long pageNum, long pageSize, Long roleId, String bindStatus, String condition) {
        IPage<RoleUserVo> page = new Page<>(pageNum,pageSize);
        page = uacRoleUserDao.getRoleUserWithPage(page,roleId,bindStatus,condition);
        return page;
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        if (null == userId) {
            throw new UacBizException(ErrorCodeEnum.UAC10011001);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("user_id",userId);

        return this.removeByMap(map);
    }

}